package cn.lanqiao.ui;

import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChangePassFrm extends JDialog {

	private static final long serialVersionUID = 1L;
	private JLabel lblPhoneNum;
	private JTextField textPhoneNum;
	private JPasswordField NewPasswordNum1;
	private JPasswordField NewPasswordNum2;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChangePassFrm dialog = new ChangePassFrm();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public ChangePassFrm() {
		setTitle("修改密码");
		setResizable(false);
		setBounds(100, 100, 420, 330);
		
		
		//窗体居中
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		lblPhoneNum = new JLabel("手机号码");
		lblPhoneNum.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		lblPhoneNum.setBounds(40, 40, 80, 30);
		getContentPane().add(lblPhoneNum);
		
		textPhoneNum = new JTextField();
		textPhoneNum.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		textPhoneNum.setBounds(140, 40, 220, 30);
		//只读
		textPhoneNum.setEnabled(false);
		getContentPane().add(textPhoneNum);
		textPhoneNum.setColumns(10);
		
		JLabel lblNewPasswordNum1 = new JLabel("新 密 码");
		lblNewPasswordNum1.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		lblNewPasswordNum1.setBounds(40, 100, 80, 30);
		getContentPane().add(lblNewPasswordNum1);
		
		NewPasswordNum1 = new JPasswordField();
		NewPasswordNum1.setBounds(140, 100, 220, 30);
		getContentPane().add(NewPasswordNum1);
		
		JLabel lblNewPasswordNum2 = new JLabel("再次确认");
		lblNewPasswordNum2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		lblNewPasswordNum2.setBounds(40, 160, 80, 30);
		getContentPane().add(lblNewPasswordNum2);
		
		NewPasswordNum2 = new JPasswordField();
		NewPasswordNum2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		NewPasswordNum2.setBounds(140, 160, 220, 30);
		getContentPane().add(NewPasswordNum2);
		
		JButton btnOK = new JButton("确认");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String telephone = textPhoneNum.getText();
				String password1 =new String ( NewPasswordNum1.getPassword());
				String password2 =new String ( NewPasswordNum2.getPassword());
				cn.lanqiao.service.UserService service = new cn.lanqiao.service.impl.UserServiceImpl();
				if(password1.equals(password2)) {				
					if(service.newPassword(telephone, password1)>0) {
						ChangePassFrm.this.setVisible(false);
					}	
				}else {
					NewPasswordNum1.setText(null);
					NewPasswordNum2.setText(null);
					JOptionPane.showMessageDialog(null, "密码不一致，请重新输入！");
				}
				
			}
		});
		btnOK.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnOK.setBounds(240, 220, 120, 30);
		getContentPane().add(btnOK);
	}
	
	public ChangePassFrm(String telephone) {
		this();
		this.textPhoneNum.setText(telephone);
	}
}
